<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/25
 * Time: 17:19
 */

namespace app\api\controller;
use think\Controller;
use think\Db;

class Driver extends Base
{
    //登录
    public function login(){
        $p = input('post.');

        if(request()->isPost()) {

            $where['username'] = $p['username'];
            $where['state'] = '1';  //1-启用
            $user = Db::name('user')->where($where)->find();

            if($p['token']){

                $p['token'] = $user['password'];
                return $this->msg_rtn(1);
            }else {

                if(!$user){

                    return $this->msg_rtn(0,'无此司机信息/已禁用！');
                }else{

                    if(CorePwd($p['password'],$user['salt']) == $user['password']){

                        $dr_where['user_id'] = $user['user_id'];
                        $info = Db::name('driver')->where($dr_where)->field('driver_id,name AS driver_name,phone AS driver_phone')->find();
                        $info['user_id'] = $user['user_id'];
                        $info['role_id'] = $user['role_id'];
                        $info['token'] = $this->CorePwd($p['password'],$user['salt']);

                        return $this->rtn_data(1,$info);

                    }else{
                        return $this->msg_rtn(0,'密码错误！');
                    }
                }
            }

        }else{
            return $this->msg_rtn(0,'提交方式错误！');
        }
    }

    //待执行订单列表 -- 订单详情
    public function wait_execute_lst(){
        $p = input('post.');
        $where['or.driver_id'] = $p['driver_id'];
        $where['or.order_state'] = '5';

        //获取待执行的订单
        $info = (array)Db::name('order')->alias('or')
            ->join('passenger pr','or.passenger_id = pr.passenger_id')
            ->where($where)
            ->field('or.order_no,or.booking_type,or.approval_time,or.departure,or.destination,or.order_state,pr.name AS passenger_name,pr.phone AS passenger_phone')
            ->select();
        $this->rtn_data(1,$info);
    }

    //申请改派
    public function approval_change_send(){

    }

    //司机操作订单
    public function operation_order(){
        $p = input('post.');

        //订单状态，1：待审批，2：已批准，3：已拒绝，4：待用车，5：已派车，6：接驾中，
        //7：已到达出发地，8：已到达目的地，9：待支付，10：待评价，11：已完成，12：已取消，13：已终止，14：无效

        $date_time = date('Y-m-d H:i:s',time());
        if($p['order_state'] == '6'){
            $data['orders_time'] = $date_time;
            $data['order_state'] = '6';

        }elseif ($p['order_state'] == '7'){
            $data['arrive_time'] = $date_time;
            $data['order_state'] = '7';

            //计算费用  -- 确认到达 -- 已到达
        }elseif ($p['order_state'] == '8'){
            $data['order_state'] = '8';

        }

        $resl = Db::name('order')->where('order_no',$p['order_no'])->update($data);
        if($resl){
            $this->msg_rtn(1);
        }else{
            $this->msg_rtn(0,$msg='系统异常，操作失败！');
        }

    }





}